/*
 * NewDemoContainer.fx
 */

package org.frapwidgets.scene.shape.demo;

import javafx.scene.Node;
import javafx.scene.layout.Resizable;
import javafx.scene.layout.Container;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
import javafx.scene.paint.Paint;
import javafx.scene.paint.Color;
import org.jfxtras.scene.shape.ResizableRectangle;

/**
 * @author Jonathan Merritt
 */

public class NewDemoContainer extends Control {

  public var contentNode: Node;
  public-init var arcRadius: Number = 15;
  public-init var background: Paint = Color.WHITE;
  public-init var leftSpace: Number = 50;

  override function create(): Node {
    skin = NewDemoContainerSkin {
      arcRadius: arcRadius
      background: background
      leftSpace: leftSpace
    };
    super.create();
  }

}


public class NewDemoContainerSkin extends Skin {
  public var demo = bind control as NewDemoContainer;

  public-init var leftSpace: Number;
  public-init var arcRadius: Number;
  public-init var background: Paint;

  def cornerBreakoutNew = CornerBreakoutNew {
    arcRadius: arcRadius
    cache: true
  }

  def bgRect = ResizableRectangle {
    fill: background
    arcHeight: 2 * arcRadius
    arcWidth: 2 * arcRadius
    cache: true
  }

  def boundWidth = bind demo.width on replace {
    bgRect.width = boundWidth;
    if (demo.contentNode instanceof Resizable) {
      (demo.contentNode as Resizable).width = boundWidth - leftSpace;
      demo.contentNode.layoutX = leftSpace;
    }
  }
  def boundHeight = bind demo.height on replace {
    bgRect.height = boundHeight;
    if (demo.contentNode instanceof Resizable) {
      (demo.contentNode as Resizable).height = boundHeight;
    }
  }

  init {
    node = Container {
      content: bind [ bgRect, cornerBreakoutNew, demo.contentNode ]
    }
  }

  override function contains(localX: Number, localY: Number): Boolean {
    node.contains(localX, localY)
  }

  override function intersects(
    localX: Number,
    localY: Number,
    localWidth: Number,
    localHeight: Number
  ): Boolean {
    node.intersects(localX, localY, localWidth, localHeight)
  }

}
